/*
 
 Copyright (c) 2010 Xiotex Studios Ltd.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The end-user documentation included with the redistribution, if any, must
 include the following acknowledgment: "This product includes software
 developed by Xiotex Studios Ltd (http://xiotexstudios.com)", in the same
 place and form as other third-party acknowledgments. Alternately, this
 acknowledgment may appear in the software itself, in the same form and
 location as other such third-party acknowledgments.
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. 
 
 */

#ifndef __CSWFSTREAM_H__
#define __CSWFSTREAM_H__

#include <math.h>
#include <string>
#include "SWFTypes.h"


class CSWFStream
{

public:
	CSWFStream();
	~CSWFStream();
	bool Initialise(std::string strPath);
	void ShutDown();
	void StartStream(swf_stream pStream);
	long GetSignedBits(DWORD dwNumBits);
	DWORD GetBits(DWORD dwNumBits);
	void GetShortTag(PSWF_TAG_SHORT pTag, swf_stream pStream);
	DWORD GetWORD(swf_stream pStream);
	DWORD GetDWORD(swf_stream pStream);
	unsigned char GetBYTE(swf_stream pStream);
	void GetLongTag(PSWF_TAG_LONG pTag, swf_stream pStream);
	DWORD GetRect(PSWF_RECT rect, swf_stream pStream);
	void ExtractRGBA(PSWF_RGBA pRGBA, swf_stream *pStream);
	void ExtractRGB(PSWF_RGBA pRGBA, swf_stream *pStream);
	void ExtractMatrix(PSWF_MATRIX_RECORD pMatrix, swf_stream *pStream);
	void ExtractColourTransformRGBA(PSWF_COLOUR_TRANSFORM pRGBA, swf_stream *pStream);
	void ExtractColourTransformRGB(PSWF_COLOUR_TRANSFORM pRGBA, swf_stream *pStream);
	void ExtractPlaceObject2(PSWF_TAG pTag, swf_stream pStream);
	void ExtractRemoveObject(PSWF_TAG pTag, swf_stream pStream);
	void ExtractDefineShape(PSWF_TAG pTag, swf_stream pStream);
	void ExtractDefineSprite(PSWF_TAG pTag, swf_stream pStream, CSWFTagList* pTagList);


public:
	inline swf_stream GetStreamBeginning() { return m_pBuffer; }
	inline DWORD GetNumBytesRead() { return m_dwNumBytes; }
	
	inline void SetMatrix(float fScaleX, float fScaleY, float fRotSkew0, float fRotSkew1, float fX, float fY)
	{
		m_mat[0][0] = fScaleX;
		m_mat[1][0] = fRotSkew0;
		m_mat[0][1] = fRotSkew1;
		m_mat[1][1] = fScaleY;
		m_mat[0][2] = fX;
		m_mat[1][2] = fY;
	}
	
	inline float GetScaleX()
	{
		return sqrtf(m_mat[0][0] * m_mat[0][0] + m_mat[1][0] * m_mat[1][0]);
	}
	
	inline float GetScaleY()
	{
		return sqrtf(m_mat[1][1] * m_mat[1][1] + m_mat[0][1] * m_mat[0][1]);
	}
	
	inline float GetAngle()
	{
		return atan2f(m_mat[1][0], m_mat[0][0]);
	}
	
	inline float GetTranslateX()
	{
		return m_mat[0][2];
	}
	
	inline float GetTranslateY()
	{
		return m_mat[1][2];
	}

protected:
	FILE*			m_pIn;
	swf_stream		m_pBuffer;
	DWORD			m_dwLength;
	swf_stream		m_pStream;
	swf_stream		m_pStreamStart;
	DWORD			m_dwBitPosition;
	DWORD			m_dwBitBuffer;
	DWORD			m_dwNumBytes;
	float			m_mat[2][3];

	
};

#endif